export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        try {
            const { sheet, col: deleteCol, table } = options;
            sheet.suspendPaint();
            const { row: dataRow } = table.dataRange();
            if (isUndo) {
                const { name, row, col, rowCount, colCount, columns, cellTypes, bindingPath, showHeader } = options.old;

                sheet.tables.remove(name);
                const oldTable = sheet.tables.add(name, row, col, rowCount, colCount, undefined, { showHeader });
                oldTable.filterButtonVisible(false);
                oldTable.bindColumns(columns.map(column => new GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField)));
                oldTable.bindingPath(bindingPath);
                cellTypes.forEach((cellType, index) => {
                    sheet.getCell(dataRow, col + index)
                        .cellType(cellType)
                        .textIndent(2.5)
                        .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
                        .backgroundImage(cellType.getFieldTypeImage());
                });
            } else {
                const { row, col, name, rowCount, colCount, columns, bindingPath, showHeader } = table.toJSON();
                options.old = {
                    name, row, col, rowCount, colCount, columns, bindingPath, showHeader,
                    cellTypes: columns.map((d, index) => {
                        return sheet.getCell(dataRow, col + index).cellType();
                    })
                };

                const newColumns = [];
                const newCellTypes = [];
                columns.forEach((column, index) => {
                    const tempCol = col + index;
                    const cell = sheet.getCell(dataRow, tempCol);
                    if (tempCol !== deleteCol) {
                        newColumns.push(column);
                        newCellTypes.push(cell.cellType());
                    }
                });

                sheet.tables.remove(name);
                if (colCount > 1) {
                    const oldTable = sheet.tables.add(name, row, col, rowCount, colCount - 1, undefined, { showHeader });
                    oldTable.filterButtonVisible(false);
                    oldTable.bindColumns(newColumns.map(column => new GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField)));
                    oldTable.bindingPath(bindingPath);
                }

                newCellTypes.forEach((cellType, index) => {
                    sheet.getCell(dataRow, col + index).cellType(cellType)
                        .textIndent(2.5)
                        .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
                        .backgroundImage(cellType.getFieldTypeImage());
                });
                sheet.getCell(dataRow, col + colCount - 1)
                    .cellType(undefined)
                    .textIndent(0)
                    .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
                    .backgroundImage(undefined);
            }
            sheet.resumePaint();
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
    }
};